package org.feifei.basic.service.impl;

import org.feifei.basic.domain.BaseDomain;
import org.feifei.basic.mapper.BaseMapper;
import org.feifei.basic.query.BaseQuery;
import org.feifei.basic.service.BaseService;
import org.feifei.basic.util.PageList;
import org.feifei.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * BaseService 的实现类，实现了所有的公共方法
 */
@Transactional(readOnly = false, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T extends BaseDomain> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    /** 查询所有 */
    @Override
    public List<T> list() {
        return baseMapper.list();
    }

    /** 根据id获取 */
    @Override
    public T get(Long id) {
        return baseMapper.get(id);
    }

    /** 添加方法或者修改的方法 */
    @Override
    @Transactional
    public void save(T t) {
        if (t.getId() == null) baseMapper.save(t);
        else baseMapper.update(t);
    }

    /** 根据id删除 */
    @Override
    @Transactional
    public void remove(Long id) {
        baseMapper.remove(id);
    }

    /** 修改方法 */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    /** 基于分页的高级查询，包含了总条数，以及查询的数据 */
    @Override
    public PageList listByPage(BaseQuery query) {
        // 查询所有的记录数量
        Long number = baseMapper.listByPageConut(query);
        if (number <= 0) {
            return new PageList();
        }
        List<T> list = baseMapper.listByPage(query);
        // 将数据封装返回
        return new PageList(number, list);
    }

    @Override
    public void batchRemove(List<Long> ids) {
        baseMapper.batchRemove(ids);
    }

 
}
